/**
 * FileName: MyController
 * Author:   xing.yang@vcg.com
 * Date:     2020/5/27 13:39
 * Description:
 */
package com.vcg.vdam.controller;

import com.vcg.vdam.aop.ResultResponse;
import com.vcg.vdam.aop.VdamAnnotation;
import com.vcg.vdam.config.*;
import com.vcg.vdam.redisLock.LockAction;
import com.vcg.vdam.testbeandefinitioHttpApiInvoke.Member;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.vcg.vdam.testbeandefinitioHttpApiInvoke.Order;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
public class MyController {

    @Autowired
    private Classes classes;

    @Autowired
    private Grade grade;

    @Autowired
    private Order order;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private Member member;

    @Autowired
    private TestBean4 testBean4;

    @Autowired
    private TestBean5 testBean5;

    @GetMapping(value = "/firstController")
    @VdamAnnotation
    public String firstController(){
        System.out.println(classes.getNumber());
        System.out.println(grade.getGradeString());
        System.out.println(testBean4.getName());
        System.out.println(testBean5.getName());
        return "hello world";
    }

    @GetMapping(value = "/testRedisLock1")
    @VdamAnnotation
    @LockAction(value = "testRedisLock")
    public String testRedisLock1(){

        try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello world";
    }

    @GetMapping(value = "/testRedisLock2")
    @VdamAnnotation
    @LockAction(value = "testRedisLock")
    public String testRedisLock2(){

        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello world";
    }

    //入参例如：{"id":345}
    @PostMapping(value = "/testRedisLock3")
    @VdamAnnotation
    @LockAction(value = "#grade.id")
    public String testRedisLock3(@RequestBody Grade grade){

        try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello world";
    }

    @PostMapping(value = "/testRedisLock4")
    @VdamAnnotation
    @LockAction(value = "#grade.id")
    public String testRedisLock4(@RequestBody Grade grade){

        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello world";
    }

    @GetMapping(value = "/testBeanFactory")
    @VdamAnnotation
    public String testBeanFactory(){
        order.testOrder();
        member.testMember();
        return null;
    }

    @GetMapping(value = "/test")
    public ResultResponse<String> test(HttpServletRequest request) {
//        String username = com.bj58.sso.client.SSOClient.getLoginName(request);
        RLock lock = redissonClient.getLock("yangxing");
        try {
            //智能时tryLock才可以生效
            lock.tryLock(1, TimeUnit.SECONDS);
//        lock.lock(4, TimeUnit.SECONDS);
            Thread.sleep(10000);
            System.out.println("当前仍然锁还在1");
//            lock.unlock();
            if(lock.isHeldByCurrentThread()){
                System.out.println("当前仍然锁还在2");
                lock.unlock();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        String username = "12";

        return new ResultResponse().setData("");
    }
}
